package com.xyc.merchant.order;

import com.github.wxpay.sdk.WXPayUtil;
import com.xyc.merchant.order.service.OrderService;
import io.goeasy.GoEasy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("merchant1")
public class MerchantOrderController {
    @Resource
    private OrderService orderService;
    @RequestMapping("notify_url")
    public void notifiUrl(HttpServletRequest request, HttpServletResponse response){

        try {
            ServletInputStream inputStream = request.getInputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while ((len = inputStream.read(bytes)) != -1){
                stringBuilder.append(new String(bytes,0,len));
            }
            System.out.println(stringBuilder.toString());

            try {
                Map<String, String> xmlToMap = WXPayUtil.xmlToMap(stringBuilder.toString());
                String trade_no = xmlToMap.get("out_trade_no");
                orderService.updateOrderState(1,trade_no);
            } catch (Exception e) {
                e.printStackTrace();
            }
            GoEasy goEasy = new GoEasy("http://rest-hangzhou.goeasy.io", "BC-f6593166466b4bf88eaeddf169f766ae");
            goEasy.publish("xy123456","pay-success");

            //需要告诉微信：收到消息
            response.getWriter().println("<xml>\n" +
                    "   <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                    "   <return_msg><![CDATA[OK]]></return_msg>\n" +
                    "   <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
                    "   <mch_id><![CDATA[10000100]]></mch_id>\n" +
                    "   <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>\n" +
                    "   <openid><![CDATA[oUpF8uMuAJO_M2pxb1Q9zNjWeS6o]]></openid>\n" +
                    "   <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>\n" +
                    "   <result_code><![CDATA[SUCCESS]]></result_code>\n" +
                    "   <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>\n" +
                    "   <trade_type><![CDATA[JSAPI]]></trade_type>\n" +
                    "</xml>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
